package com.imooc.mall.controller;

import com.imooc.mall.common.ApiRestResponse;

import com.imooc.mall.common.Constant;
import com.imooc.mall.exception.ImoocMallException;
import com.imooc.mall.exception.ImoocMallExceptionEnum;
import com.imooc.mall.model.pojo.User;
import com.imooc.mall.service.CategoryService;
import com.imooc.mall.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;
import java.security.NoSuchAlgorithmException;

/*
* 描述： 用户控制器
* */
@Controller
public class UserController {
    @Resource
    private UserService userService;



    @ApiOperation("测试")
    @GetMapping("/test")
    @ResponseBody
    public User personalPage(){
        return userService.getUser();
    }

    @PostMapping("/register")
    @ResponseBody
    public ApiRestResponse register(@RequestParam("userName") String username , @PathParam("password") String password) throws ImoocMallException {
        // userService.
        //先进行基本的判断然后再进行下一次的校验
        if(StringUtils.isEmpty(username)){
            return ApiRestResponse.error(ImoocMallExceptionEnum.ENUM_USER_NUME);
        }
        if (StringUtils.isEmpty(password)){
            return ApiRestResponse.error(ImoocMallExceptionEnum.ENUM_USER_PASSWORD);
        }
        if(password.length()<8){
            return ApiRestResponse.error(ImoocMallExceptionEnum.ENUM_PASSWORD_SHORT);
        }

        userService.register(username, password);
        return ApiRestResponse.success();
    }

    //登录的
    @PostMapping("/login")
    @ResponseBody
    public ApiRestResponse login(@RequestParam("userName") String username, @RequestParam("password") String password, HttpSession session) throws ImoocMallException, NoSuchAlgorithmException {
        if(StringUtils.isEmpty(username)){
            return ApiRestResponse.error(ImoocMallExceptionEnum.ENUM_USER_NUME);
        }
        if (StringUtils.isEmpty(password)){
            return ApiRestResponse.error(ImoocMallExceptionEnum.ENUM_USER_PASSWORD);
        }
        User user = userService.login(username, password);
        // 保存用户信息时，不保存密码。返回的时候
        user.setPassword(null);
        session.setAttribute(Constant.IM00C_MALL_USER,user);
        return ApiRestResponse.success(user);
    }

    // 修改用户信息的signature
    @PostMapping("/user/update")
    @ResponseBody
    public ApiRestResponse update(@RequestParam("signature") String signature,HttpSession session) throws ImoocMallException {
        User user =(User) session.getAttribute(Constant.IM00C_MALL_USER);
        if(user == null)
        {
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_LOGIN);
        }
        userService.update(user,signature);
        return ApiRestResponse.success();
    }

    /*
    *  退出登录
    * */
    @PostMapping("/user/logout")
    @ResponseBody
    public ApiRestResponse logout (HttpSession session){
        session.removeAttribute(Constant.IM00C_MALL_USER);
        return ApiRestResponse.success();
    }


    // 1-代表为用户 2-代表管理员
    @PostMapping("/adminLogin")
    @ResponseBody
    public ApiRestResponse adminLogin(@RequestParam("userName") String username, @RequestParam("password") String password, HttpSession session) throws ImoocMallException, NoSuchAlgorithmException {
        if(StringUtils.isEmpty(username)){
            return ApiRestResponse.error(ImoocMallExceptionEnum.ENUM_USER_NUME);
        }
        if (StringUtils.isEmpty(password)){
            return ApiRestResponse.error(ImoocMallExceptionEnum.ENUM_USER_PASSWORD);
        }
        User user = userService.login(username, password);

        //校验是不是管理员
        if (userService.checkAdminRole(user)) {
            //是管理员，执行操作
            //保存用户信息不保存，密码
            user.setPassword(null);
            session.setAttribute(Constant.IM00C_MALL_USER,user);
            return ApiRestResponse.success(user);

        }
        // 保存用户信息时，不保存密码。返回的时候
       else {
           return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_ADMIN);
        }
    }


}